<?php
namespace app\admin\controller;
use think\Db;
class Emailtp extends Base
{   
    public function lists(){
        //ajax查询数据
        if( is_post() ){
            $page     = input('page');
            $status   = input('status');
            $keyword  = input('keyword');
            $min_time = input('min_time');
            $max_time = input('max_time');
            
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    email_tp a
                left join
                    email_ac b
                on
                    a.emailac_id = b.id 
                where 
                    1 = ?
                ";
            $param = [1];
            if ( $status != '100all' ) {
                $sql .= " and a.status = ?";
                $param[] = $status;
            }
            if ( !empty($keyword) ) {
                $sql .= " and (note a.like ? or a.subject like ?)";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
            }
            if ( !empty($min_time) ) {
                $sql    .= " and a.create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and a.create_time <= ?";
                $param[] = strtotime($max_time);
            }
            #分页
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            $page_size   = c('page_size');
            $page        = com_cur_page($total, $page,  $page_size);
			$page_data   = com_page_data($total, $page,  $page_size);
            
            //列表数据
			$start       = ($page - 1) * $page_size;
			$sql        .= " order by a.sort desc, a.id desc limit $start, $page_size";
			$field       = "a.*, b.email";
			$sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            //格式数据
            foreach($list as $k => $v){                          
                $list[$k]['checkbox']       =  com_format_checkbox($v['id']);
                $list[$k]['create_time']    =  com_format_time( $v['create_time'] );
                $list[$k]['sort']           =  com_page( ['url'=>url("c/edit"),'val'=>$v['sort'],'post'=>['t'=>'email_tp','id'=>$v['id'],'f'=>'sort']],'input' );
                $list[$k]['status']         =  com_page( ['url'=>url("c/edit"),'val'=>$v['status'],'post'=>['t'=>'email_tp','id'=>$v['id'],'f'=>'status']],'iphone' );
                
                $list[$k]['body']          =  com_substr(strip_tags($v['body']), 30);
                
                //操作按钮
                $list[$k]['action']          =  com_page(['title'=>'编辑邮件模板','url'=>url("emailtp/edit","id={$v['id']}"),'ico'=>'edit']);
                $list[$k]['action']         .=  com_page(['title'=>'拷贝邮件模板','url'=>url("c/copy"),'post'=>['t'=>'email_tp','id'=>$v['id']],'ico'=>'copy']);
                $list[$k]['action']         .=  com_page(['title'=>'删除邮件模板','url'=>url("c/del"),'post'=>['t'=>'email_tp','id'=>$v['id']],'ico'=>'del3', 'confirm'=>"你确定要删除[{$v['title']}]吗?"]);
            }
			$com_body    = com_build_table($list, array("checkbox","id","note","uuid", "email","addresscc","subject", "body","attach","sort","status" ,"create_time", "action"));
			$com_page    = com_build_page( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            com_check(['data'=>$data,'suc'=>['js_action'=>'function.iphone']]);
        } 
        $data = [
            'status'    => ['100all'=>'全部状态'] + c('type.status'),
            'nav'       => ['邮件模板', '列表'],
        ];
        return view('', $data);
    }
    
    //添加
    public function add(){
        if( is_post() ){
            $d = com_data(input());
                
            $rule = [
                   'uuid'           =>  'require|unique:email_tp',
                   'emailac_id'     =>  'number',
                   'subject'        =>  'require',
                   'body'           =>  'require',
                ];
            $msg = [
                   'uuid.require'       =>  '邮件标识不能为空',
                   'uuid.unique'       =>  '邮件标识不能重复',
                   'emailac_id.number' =>  '发送邮件账号不能为空',
                   'subject.require'    =>  '邮件主题不能为空',
                   'body.require'       =>  '邮件内容不能为空',
                ];
            com_validate( $rule, $msg, $d, true );
            $d['create_time'] = time();
            Db::name('email_tp')->insert($d);
            $config = [
                'suc' => [
                    'msg' => '添加模板成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }    
        $d = [
            'emailac'   =>['100all'=>['id'=>'100all','email'=>'请选择发送账号']] + Db::name('email_ac')->where('status',0)->order("sort desc, id desc")->select(),
            'status'    => c('type.status'),
        ];
        return view('', $d);
    }
    
    
    //编辑
    public function edit( $id ){
        if( is_post() ){
            $d = com_data(input());
            $rule = [
                   'uuid'           =>  'require|unique:expand,uuid,'.$id,
                   'emailac_id'     =>  'require',
                   'subject'        =>  'require',
                   'body'           =>  'require',
                ];
            $msg = [
                   'uuid.require'       =>  '邮件标识不能为空',
                   'uuid.unique'       =>  '邮件标识不能重复',
                   'emailac_id.require' =>  '发送邮件账号不能为空',
                   'subject.require'    =>  '邮件主题不能为空',
                   'body.require'       =>  '邮件内容不能为空',
                ];
            com_validate( $rule, $msg, $d, true );
            $d['id'] = $id;
            $d['update_time'] = time();
            Db::name('email_tp')->update($d);
            $config = [
                'suc' => [
                    'msg' => '编辑模板成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }
        $data = [
            'emailac'   =>['100all'=>['id'=>'100all','email'=>'请选择发送账号']] + Db::name('email_ac')->where('status',0)->order("sort desc, id desc")->select(),
            'info'      => Db::name('email_tp')->find($id),
        ];
        return view('', $data);
    }
}